package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.House;
import com.ruoyi.system.domain.Shopping;
import com.ruoyi.system.service.IHouseService;
import com.ruoyi.system.service.IShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车Controller
 *
 * @author ...
 * @date 2022-03-18
 */
@RestController
@RequestMapping("/system/shopping")
public class ShoppingController extends BaseController {
    @Autowired
    private IShoppingService shoppingService;
    @Autowired
    private IHouseService houseService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Shopping shopping) {
        startPage();
        List<Shopping> list = shoppingService.selectShoppingList(shopping);
        return getDataTable(list);
    }

    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(shoppingService.selectShoppingById(id));
    }

    /**
     * 新增购物车
     */
    @PostMapping
    public AjaxResult add(@RequestBody Shopping shopping) {
        House house = houseService.selectHouseById(shopping.getHouseId());
        shopping.setHouseName(house.getName());
        shopping.setCreateUser(SecurityUtils.getUserId().intValue());
        shopping.setCreateTime(DateUtils.getNowDate());
        return toAjax(shoppingService.insertShopping(shopping));
    }

    /**
     * 查询我的购物车
     * @return
     */
    @GetMapping("/myShop")
    public AjaxResult getMyShop(){
        List<Shopping> myShop = shoppingService.getMyShop();
        return AjaxResult.success(myShop);
    }

    /**
     * 修改购物车
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Shopping shopping) {
        shopping.setUpdateUser(SecurityUtils.getUserId().intValue());
        shopping.setUpdateTime(DateUtils.getNowDate());
        return toAjax(shoppingService.updateShopping(shopping));
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(shoppingService.deleteShoppingByIds(ids));
    }
}
